package com.wn.controller;

import com.wn.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yellow Docter
 * @date 2022 -07-11
 * @desc
 */
@Controller
@RequestMapping("user")
@CrossOrigin
public class UserController {

    /**
     *  页面发送了请求,  findByid 的url找到 方法:  findUser
     *  进行参数绑定, 直接返回user,      @ResponseBody 这个注解会
     *  把user对象转成json 字符串
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("findByid")
    public User findUser(Integer id){

        System.out.println("id = " + id);

        System.out.println("调用服务器 查询数据库中数据 ");

        User user = new User();
        user.setName("zhangsan");
        user.setAge(30);

        return user;

    }

    // List<User> java的集合对象 可以直接转成 json 字符串
    @ResponseBody
    @RequestMapping("list")
    public List<User> listUser(Integer id){

        User user1 = new User();
        user1.setName("王语嫣");
        user1.setAge(20);

        User user2 = new User();
        user2.setName("白刀凤");
        user2.setAge(24);

        User user3 = new User();
        user3.setName("秦红棉");
        user3.setAge(24);


        User user4 = new User();
        user4.setName("李青萝");
        user4.setAge(24);

        List<User> userList = new ArrayList<User>();
        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        userList.add(user4);
        return userList;
    }

}
